﻿<from class="layui-form layui-form-pane" action="" lay-filter="form-userinfo" id="form-userinfo">
    <div class="layui-form-item">
        <label class="layui-form-label">头像</label>
        <div class="layui-input-block">
            <button type="button" class="layui-btn" id="test3"><i class="layui-icon">&#xe67c;</i>上传头像</button>
            <img src="../photo/get/0"  class="layui-nav-img user-photo" style="width: 35px;height: 35px;">
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">姓名</label>
        <div class="layui-input-block">
            <input type="text" name="name" lay-verify="name" autocomplete="off"
                   placeholder="请输入姓名" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">邮箱</label>
        <div class="layui-input-block">
            <input type="text" name="email" lay-verify="email" autocomplete="off" placeholder="请输入邮箱"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">手机</label>
        <div class="layui-input-block">
            <input type="text" name="phone" lay-verify="phone" autocomplete="off" placeholder="请输入手机"
                   class="layui-input">
        </div>
    </div>
    <div class="layui-form-item" pane>
        <label class="layui-form-label">性别</label>
        <div class="layui-input-block">
            <input type="radio" name="sex" value="male" title="男" checked="checked">
            <input type="radio" name="sex" value="female" title="女">
            <input type="radio" name="sex" value="hide" title="保密">
        </div>
    </div>

    <div class="layui-form-item layui-form-text">
        <label class="layui-form-label">签名</label>
        <div class="layui-input-block">
            <textarea placeholder="请输入内容" name="sign" class="layui-textarea"></textarea>
        </div>
    </div>

    <div class="layui-form-item" style="text-align: center;">
        <button class="layui-btn" lay-submit="" lay-filter="userinfo">确认修改</button>
    </div>
</from>
<script type="text/javascript">

    layui.define(['layer', 'form', 'layedit', 'upload'], function () {
        var form = layui.form;
        var $ = layui.jquery;
        var upload = layui.upload;
        form.render();
        upload.render({ //允许上传的文件后缀
            elem: '#test3'
            , url: '../user/upload'
            , accept: 'file' //普通文件
            ,size: 2048
            , exts: 'jpg|png' //只允许上传压缩文件
            , done: function (res) {
                $(".user-photo").attr("src","../photo/get/"+Math.ceil(Math.random()*-100));
            }
        });


        $.getJSON("../user/get/0", function (data) {
            workUtils.fillForm($("#form-userinfo"), data);
            $("[name='sex'][value='" + data.sex + "']").prop("checked", "checked");
            form.render(); //这个很重要
        });


        //自定义验证
        form.verify({
            newpassword: [/^[\S]{6,12}$/, '密码必须6到12位']
            , password: function (value) {
                var repassvalue = $('#newpassword').val();
                if (value != repassvalue) {
                    return '两次输入的密码不一致!';
                }
            }
        });

        form.on('submit(userinfo)', function (data) {
            var index = layer.load(1);
            setTimeout(function () {
                workUtils.ajaxPost("../user/editinfo",data.field,function (result) {
                    layer.close(index);
                    workUtils.msg(result);
                });
            }, 400);
            return false;
        });
    });
</script>